package com.ace3l.legal.gwt.domainmodule.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.ace3l.legal.gwt.domainmodule.client.UserService;
import com.ace3l.legal.gwt.domainmodule.gwt.GWTLawFirm;
import com.ace3l.legal.gwt.domainmodule.gwt.GWTStudent;
import com.ace3l.legal.gwt.domainmodule.domainxform.LawFirmXForm;
import com.ace3l.legal.gwt.domainmodule.domainxform.StudentXForm;
import com.ace3l.legal.backend.dao.UserDao;
import com.ace3l.legal.backend.dao.LawFirmDao;
import com.ace3l.legal.backend.dao.StudentDao;
import com.ace3l.legal.backend.domain.LawFirm;
import com.ace3l.legal.backend.domain.User;
import com.ace3l.legal.backend.domain.Role;
import com.ace3l.legal.backend.domain.Student;
import com.ace3l.legal.Constants;
import com.synesthesiaconsulting.gwt.resources.client.MsgObjBundle;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Created by IntelliJ IDEA.
 * User: ageer
 * Date: Jul 22, 2008
 * Time: 10:24:26 AM
 * To change this template use File | Settings | File Templates.
 */
public class UserServiceImpl extends RemoteServiceServlet implements UserService {
    public Boolean authenticateUser(String username, String password) {
        WebApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        UserDao userDao = (UserDao)appCtx.getBean("userDao");

        Boolean isAuth = userDao.authenticateUser(username, password);
        if (isAuth) {
            User myUser = userDao.getUser(username);
            this.getThreadLocalRequest().getSession().setAttribute(Constants.ISAUTH, Boolean.TRUE);
            this.getThreadLocalRequest().getSession().setAttribute(Constants.USERPROFILE, myUser);
            this.getThreadLocalRequest().getSession().setAttribute(Constants.USERROLE, myUser.getFirstRole());
        }
        return isAuth;
    }

    public MsgObjBundle createLawFirm(GWTLawFirm lawFirm) {
        WebApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        UserDao userDao = (UserDao)appCtx.getBean("userDao");
        LawFirmDao lawDao = (LawFirmDao)appCtx.getBean("lawFirmDao");

        if (doesUserExist(lawFirm.getUser().getUsername())) {
            return new MsgObjBundle(lawFirm, Constants.USEREXISTS);
        }
        LawFirm myLawFirm = LawFirmXForm.fromGWT(lawFirm);
        myLawFirm.setEmailVerified(Boolean.FALSE);
        myLawFirm.setLawFirmStatus("NEW");
        User myUser = myLawFirm.getUser();
        Role lawRole = new Role();
        lawRole.setRole(Role.LAWFIRM);
        myUser.addRole(lawRole);
        
        try {
            myUser = userDao.saveUser(myLawFirm.getUser());
            myLawFirm.setUser(myUser);
        } catch (Exception e) {
            return new MsgObjBundle(lawFirm, Constants.FAIL);
        }
        try {
            myLawFirm = lawDao.saveLawFirm(myLawFirm);
        } catch (Exception e) {
            //law firm failed to create back it out
            userDao.deleteUser(myUser);
            return new MsgObjBundle(lawFirm, Constants.FAIL);
        }

        return new MsgObjBundle(LawFirmXForm.toGWT(myLawFirm), Constants.SUCCESS);
    }

    public MsgObjBundle createStudent(GWTStudent student) {
        WebApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        UserDao userDao = (UserDao)appCtx.getBean("userDao");
        StudentDao studentDao = (StudentDao)appCtx.getBean("studentDao");

        if (doesUserExist(student.getUser().getUsername())) {
            return new MsgObjBundle(student, Constants.USEREXISTS);
        }

        Student myStudent = StudentXForm.fromGWT(student);
        myStudent.setEmailVerified(Boolean.FALSE);
        myStudent.setStudentStatus("NEW");
        User myUser = myStudent.getUser();
        Role lawRole = new Role();
        lawRole.setRole(Role.STUDENT);
        myUser.addRole(lawRole);

        try {
            myUser = userDao.saveUser(myStudent.getUser());
            myStudent.setUser(myUser);
        } catch (Exception e) {
            //error creating user?
            return new MsgObjBundle(student, Constants.FAIL);
        }
        try {
            myStudent = studentDao.saveStudent(myStudent);
        } catch (Exception e) {
            //law firm failed to create back it out
            userDao.deleteUser(myUser);
            return new MsgObjBundle(student, Constants.FAIL);
        }
        return new MsgObjBundle(StudentXForm.toGWT(myStudent), Constants.SUCCESS);
    }

    public GWTLawFirm getLawFirm(Integer id) {
        WebApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

        LawFirmDao lawDao = (LawFirmDao)appCtx.getBean("lawFirmDao");
        LawFirm myLawFirm = lawDao.getLawFirm(id);
        return LawFirmXForm.toGWT(myLawFirm);
    }

    
    public void logoutUser() {
        this.getThreadLocalRequest().getSession().removeAttribute(Constants.ISAUTH);
        this.getThreadLocalRequest().getSession().removeAttribute(Constants.USERPROFILE);
        this.getThreadLocalRequest().getSession().removeAttribute(Constants.USERROLE);
        this.getThreadLocalRequest().getSession().invalidate();
    }

    public String getRole() {
        String role = (String)this.getThreadLocalRequest().getSession().getAttribute(Constants.USERROLE);
        if (role == null) return Constants.NOAUTH;
        else return role;
    }

    public Boolean doesUserExist(String username) {
        WebApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        UserDao userDao = (UserDao)appCtx.getBean("userDao");

        User me = userDao.getUser(username);
        return (me != null);
    }
}